<?php

namespace app\admin\controller;

use app\admin\model\TeacherModel;
use cmf\controller\AdminBaseController;
use think\Controller;
use think\Request;
use think\Db;
use think\db\Query;

class TeacherController extends AdminBaseController
{
    public function index()
    {
        $name=$this->request->param('name');
        $data=Db::name('teacher')
            ->where(function (Query $query) use ($name){
                if ($name){
                    $query->where('name','like',"%$name%");
                }
            })
            ->order('id desc')
            ->paginate(2,false,$this->request->param());
        $page=$data->render();
        return $this->fetch('index',['page'=>$page,'teacher'=>$data]);
    }

    public function add()
    {
        return $this->fetch();
    }
    public function addPost()
    {
        $data=input();
        if (empty($data['name'])){
            $this->error('原因不能为空');
        }
        $add=TeacherModel::create($data);
        if ($add){
            $this->success('添加成功','teacher/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $teacher=TeacherModel::get($id);
        return $this->fetch('',['teacher'=>$teacher]);
    }
    public function editPost()
    {
        $data=input();
        if(empty($data['name'])){
            $this->error('原因不能为空');
        }
        $edit=TeacherModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','Teacher/index');
        }else{
            $this->error('修改失败');
        }
    }

    public function delete()
    {
        $data=input();
        $del=TeacherModel::destroy($data['id']);
        if ($del){
            $this->success('删除成功！','Teacher/index');
        }else{
            $this->error('删除失败！');
        }
    }
}
